//
//  UIDevice+SGExtension.swift
//  Swift-Codedase
//
//  Created by xyanl on 2023/7/25.
//

import UIKit
import Alamofire

extension UIDevice {
    /// 顶部安全区域高度
    static var topSafeHeight: CGFloat {
        get {
            let scene = UIApplication.shared.connectedScenes.first
            guard let windowScene = scene as? UIWindowScene else { return 0 }
            guard let window = windowScene.windows.first else { return 0 }
            return window.safeAreaInsets.top
        }
    }
    
    /// 底部安全区域高度
    static var bottomSafeHeight: CGFloat {
        get {
            let scene = UIApplication.shared.connectedScenes.first
            guard let windowScene = scene as? UIWindowScene else { return 0 }
            guard let window = windowScene.windows.first else { return 0 }
            return window.safeAreaInsets.bottom
        }
    }
    
    /// 顶部状态栏高度（包括安全区）
    static var statusBarHeight: CGFloat {
        get {
            let scene = UIApplication.shared.connectedScenes.first
            guard let windowScene = scene as? UIWindowScene else { return 0 }
            guard let statusBarManager = windowScene.statusBarManager else { return 0 }
            return statusBarManager.statusBarFrame.height
        }
    }
    
    /// 导航栏高度
    static var navBarHeight: CGFloat {
        get {
            return 44.0
        }
    }
    
    /// 状态栏+导航栏的高度
    static var navBarFullHeight: CGFloat {
        get {
            return UIDevice.navBarHeight+UIDevice.statusBarHeight
        }
    }
    
    /// 底部导航栏高度
    static var tabBarHeight: CGFloat {
        get {
            return 49.0
        }
    }
    
    /// 底部导航栏高度（包括安全区）
    static var tabBarFullHeight: CGFloat {
        get {
            return UIDevice.tabBarHeight + UIDevice.bottomSafeHeight
        }
    }
}

/// 基于Alamofire, 网络是否连接, 这个方法不建议放到这个类中,可以放在全局的工具类中判断网络链接情况
/// 用计算型属性是因为这样才会在获取 isNetworkConnect 时实时判断网络链接请求,如有更好的方法可以fork
extension UIDevice {
    static var isNetworkConnect: Bool {
        let network = NetworkReachabilityManager()
        return network?.isReachable ?? true // 无返回就默认网络已连接
    }
}
